package com.yc.servlet;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yc.bean.OrderBean;
import com.yc.bean.OrderDetailBean;
import com.yc.dao.OrderDao;
import com.yc.dao.OrderDetailDao;
import com.yc.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/showOrder")

public class ShowOrederServlet extends BaseServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");

        // 解析请求体中的 JSON 数据
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());

        JsonNode jsonNode = objectMapper.readTree(req.getReader());
        String op = jsonNode.path("op").asText();
        String userid=jsonNode.path("userid").asText();
        String order_code=jsonNode.path("order_code").asText();
        String json=null;
        if ("order".equals(op)){

            List<OrderBean> list = OrderDao.find(Integer.parseInt(userid));
            json = objectMapper.writeValueAsString(list);
        }else if ("orderDetail".equals(op)){
            List<OrderDetailBean> list = OrderDetailDao.find();
             json = objectMapper.writeValueAsString(list);
        }else if("payfor".equals(op)){
             OrderDao.update(order_code);
        }else {
            System.out.println("系统出错");
        }

        // 将 JSON 数据写入响应
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }
}
